//
//  CABasicAnimationController.m
//  CoreAnimationDemo
//
//  Created by AY on 17/5/31.
//  Copyright © 2017年 Alex. All rights reserved.
//

#import "CABasicAnimationController.h"

@interface CABasicAnimationController ()
@property (nonatomic, strong) CALayer *layer;
@end

@implementation CABasicAnimationController

- (void)viewDidLoad {
    [super viewDidLoad];

    self.view.backgroundColor = [UIColor whiteColor];
    CALayer *layer = [CALayer layer];
    layer.bounds = CGRectMake(0, 0, 100, 100);
    layer.position = CGPointMake(100, 100);
    layer.backgroundColor = [UIColor greenColor].CGColor;
    self.layer = layer;
    [self.view.layer addSublayer:layer];
}

/// 平移
- (void)translation {
    CABasicAnimation *animation = [CABasicAnimation animation];
    animation.keyPath = @"transform.translation.x";
    animation.fromValue = nil;
    animation.toValue = @100;
    animation.removedOnCompletion = NO;
    animation.fillMode = kCAFillModeForwards;
    animation.duration = 2.0;
    [self.layer addAnimation:animation forKey:nil];
}

/// 缩放
- (void)scale {
    CABasicAnimation *animation = [CABasicAnimation animation];
    animation.keyPath = @"bounds";
    animation.toValue = [NSValue valueWithCGRect:CGRectMake(0, 0, 200, 200)];
    animation.duration = 1.0;
    /**让图层保持动画执行完毕后的状态**/
    // 动画执行完毕后不要删除动画
    animation.removedOnCompletion = NO;
    // 保持最新的状态
    animation.fillMode = kCAFillModeForwards;
    [self.layer addAnimation:animation forKey:nil];
}

/// 旋转
- (void)rotate {
    CABasicAnimation *animation = [CABasicAnimation animation];
    animation.keyPath = @"transform";
    animation.toValue = [NSValue valueWithCATransform3D:CATransform3DMakeRotation(M_PI, 1, -1, 0)];
    animation.removedOnCompletion = NO;
    animation.fillMode = kCAFillModeForwards;
    animation.duration = 2.0;
    [self.layer addAnimation:animation forKey:nil];
}

/// 斜向平移
- (void)translate {
    CABasicAnimation *anim = [CABasicAnimation animation];
    anim.keyPath = @"position";
    // toValue : 最终变成什么值
    // byValue : 增加多少值
    anim.byValue = [NSValue valueWithCGPoint:CGPointMake(200, 300)];
    anim.duration = 2.0;
    anim.removedOnCompletion = NO;
    anim.fillMode = kCAFillModeForwards;
    [self.layer addAnimation:anim forKey:nil];
}


- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    [self translate];
}

@end
